package com.deyuanyun.pic.pipe.controller;

import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.pipe.controller.dto.TransformMediaQuery;
import com.deyuanyun.pic.pipe.controller.vo.TransformMediaVO;
import com.deyuanyun.pic.pipe.enumeration.PipeExportEnum;
import com.deyuanyun.pic.pipe.service.PipeMgrService;
import com.deyuanyun.pic.settings.util.DownExcelUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * “输送介质”控制类
 *
 * @author xiatao
 *时间:2015年08月25日 11时19分52秒
 * 基础数据输送介质controller
 */
@Controller
@RequestMapping("/pipe/transformMedia")
public class TransformMediaController extends PipeBaseController {

    @Resource
    private PipeMgrService pipeMgrService;

    /**
     * 跳转到 输送介质 主页面
     *
     * @return
     */
    @RequestMapping("/toTransformMedia")
    public ModelAndView toTransformMedia(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("basedata/pipe/transformMedia");
        return mv;
    }


    /**
     * 根据条件分页查询 输送介质 数组对象
     *
     * @param transformMediaQuery
     */
    @RequestMapping("/query.json")
    @ResponseBody
    public void query(TransformMediaQuery transformMediaQuery ) {
        PageJsonBean jsonBean = new PageJsonBean();
        jsonBean.setList(this.queryVO(transformMediaQuery));
        AjaxSupport.sendSuccessText("success", jsonBean);
    }

    /**
     * 根据条件导出 输送介质
     *
     * @param transformMediaQuery
     * @throws IOException
     */
    @RequestMapping("/export.json")
    @ResponseBody
    public void export(TransformMediaQuery transformMediaQuery ) throws IOException {
        List list = this.queryVO(transformMediaQuery);
        PipeExportEnum exportEnum = PipeExportEnum.TransformMedia;
        String[] columnKeys = {"pipeLineName", "mediaType", "temperature","ph", "co2",
                "cl", "density", "h2s", "h2o", "boil", "recorder", "recordTime", "remark"};
        DownExcelUtil.export(exportEnum.getFileName(), pipePath+exportEnum.getTempPath(), exportEnum.getBeginRow(), list, columnKeys);
    }

    private List queryVO(TransformMediaQuery transformMediaQuery ) {
        return new TransformMediaVO().fromDomainList(pipeMgrService.queryTransformMedia(transformMediaQuery));
    }
}
